/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

/*
 * @Author: your name
 * @Date: 2020-08-06 17:36:13
 * @LastEditTime: 2020-08-17 10:26:52
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \kuaidihelp\kuaidihelp_shop_admin\src\components\PostExpress\Address.js
 */
import React, { Component } from "react";
import { connect } from "dva";
import StandardTable from "@/components/StandardTable";
import { debounce } from "lodash";
import SearchBar from "./components/SearchBar.jsx";
import styles from "./style.less";
import { message } from "antd";

@connect(({ loading, submissionRecords }) => ({
  siteList: submissionRecords.siteList,
  listLoading: loading.effects["submissionRecords/getDispatchData"],
}))
export default class SmsList extends Component {
  state = {};
  constructor(props) {
    super(props);
    this.column = [
      {
        title: "驿站名称",
        dataIndex: "company_name",
      },
      {
        title: "联系电话",
        dataIndex: "contact_phone",
      },
      {
        title: `驿站地址`,
        dataIndex: "address",
      },
      {
        title: "开通状态",
        dataIndex: "is_open",
        render: text => (text == 1 ? "开启" : "未开启"),
      },
      {
        title: "操作",
        dataIndex: "operate",
        render: (val, item) => {
          return item.is_open == 1 && item.is_select == 1 ? (
            <a onClick={() => this.onClosePostage(item)} style={{ color: "red" }}>
              关闭
            </a>
          ) : (
            "--"
          );
        },
      },
    ];
    this.searchRef = React.createRef();
  }

  handleTableChage = debounce(
    pagination => {
      const { current } = pagination;
      this.searchRef.search(current);
      this.setState({
        current,
      });
    },
    500,
    {
      leading: true,
      trailing: false,
    }
  );

  onClosePostage = ({ cm_id }) => {
    const that = this;
    const { dispatch } = that.props;
    const { current } = that.state;
    dispatch({
      type: "submissionRecords/feeClose",
      payload: {
        cm_id,
      },
    }).then(res => {
      message.success("操作成功", 1);
      this.searchRef.search(current || 1);
    });
  };

  render() {
    const { siteList, listLoading } = this.props;
    return (
      <div className={styles.main}>
        <SearchBar wrappedComponentRef={form => (this.searchRef = form)} />
        <StandardTable
          rowKey={record => record.id}
          loading={listLoading}
          data={siteList}
          columns={this.column}
          onChange={this.handleTableChage}
        />
      </div>
    );
  }
}
